package com.baimawangzi.recovery.servlet;

import com.baimawangzi.recovery.bean.Admin;
import com.baimawangzi.recovery.service.AdminService;
import com.baimawangzi.recovery.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(111);//debug
        AdminService adminService = new AdminServiceImpl();
        //解析url参数
        String loginname=req.getParameter("loginname");
        String pwd=req.getParameter("pwd");
        Admin a = adminService.getLogig(loginname, pwd);
        if(null !=a){//登陆成功去index.jsp否则重新登陆
            req.getSession().setAttribute("admin",a);
            resp.sendRedirect("index.jsp");
        }
        else {
            req.setAttribute("msg","用户名或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);

        }
    }
}